package com.ruoyi.scale.rule.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.scale.rule.EvalRule;
import com.ruoyi.scale.document.refs.*;
import com.ruoyi.scale.document.ScaleLog;
import com.ruoyi.scale.document.Scale;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service("commonEvalRule")
public class CommonEvalRule implements EvalRule {
    @Override
    public ScaleResult eval(Scale scale, ScaleLog scaleLog) {
        //建议
        StringBuilder advice = new StringBuilder();
        //详细建议
        StringBuilder detailAdvice = new StringBuilder();
        //结论
        StringBuilder conclusion = new StringBuilder();

        BigDecimal totalScore = scaleLog.getAnswerList().stream().map(ScaleAnswer::getScore).reduce(BigDecimal.ZERO, BigDecimal::add);

        List<ScaleDomainRule> totalScoreRule = scale.getTotalScoreRule();
        //总分匹配
        collectTotalScore(advice, detailAdvice, conclusion, totalScore, totalScoreRule);
        //领域分支匹配
        List<ScaleDomainResult> resultItem = collectDomainData(scale,
                scaleLog);
        return ScaleResult
                .builder()
                .advice(advice.toString())
                .detailAdvice(detailAdvice.toString())
                .conclusion(conclusion.toString())
                .resultItem(resultItem)
                .totalScore(totalScore)
                .build();
    }

    /**
     * 收集领域结果集
     * @param scale
     * @param scaleLog
     */
    private List<ScaleDomainResult> collectDomainData(Scale scale, ScaleLog scaleLog) {
        if(CollectionUtils.isEmpty(scale.getScaleDomainData())){
            return new ArrayList<>();
        }
        List<ScaleDomainResult> resultItem = new ArrayList<>();
        //收集阶段数据
        List<ScaleDomainResult> scaleDomainResults = this.collectDomainScore(scale, scaleLog);
        for (ScaleDomainResult scaleDomainResult : scaleDomainResults) {
            ScaleDomainRule scaleDomainRule = scaleDomainResult.matchFirstDomainRule();
            if(scaleDomainRule != null){
                scaleDomainResult.setAdvice(scaleDomainRule.getAdvice());
                scaleDomainResult.setConclusion(scaleDomainRule.getConclusion());
                scaleDomainResult.setDetailAdvice(scaleDomainRule.getDetailAdvice());
                resultItem.add(scaleDomainResult);
            }
        }
        return resultItem;
    }

    /**
     * 计算总分评分规则
     *
     * @param advice
     * @param detailAdvice
     * @param conclusion
     * @param totalScore
     * @param totalScoreRule
     */
    private void collectTotalScore(StringBuilder advice,
                                   StringBuilder detailAdvice,
                                   StringBuilder conclusion,
                                   BigDecimal totalScore, List<ScaleDomainRule> totalScoreRule) {
        if (!CollectionUtils.isEmpty(totalScoreRule)) {
            for (ScaleDomainRule scaleDomainRule : totalScoreRule) {
                if (scaleDomainRule.match(totalScore)) {
                    advice.append(StringUtils.defaultIfBlank(scaleDomainRule.getAdvice(),""));
                    detailAdvice.append(StringUtils.defaultIfBlank(scaleDomainRule.getDetailAdvice(),""));
                    conclusion.append(StringUtils.defaultIfBlank(scaleDomainRule.getConclusion(),""));
                    break;
                }
            }
        }
    }
}
